package cn.edu.scau.cmi.luodongmei.gui;

import cn.edu.scau.cmi.luodongmei.domain.Insurance;
import cn.edu.scau.cmi.luodongmei.simpleFactory.InsuranceFactorys;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.geometry.Insets;
import javafx.scene.control.ChoiceBox;
import javafx.scene.control.TextArea;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;

public class SimpleFactoryGUI extends Pane {

	Insurance insurance = null;
	
	public SimpleFactoryGUI() {
		BorderPane borderPane = new BorderPane();
		borderPane.setPrefSize(500, 500);		
		
		Image image = new Image("person.jpg");
		ImageView imageView = new ImageView(image);
		imageView.setFitHeight(200);
		imageView.setFitWidth(200);
		
		Text personInfo = new Text("      姓名：罗东妹        学号：201627010116");
		
		Text selectType = new Text("      请选择保险       ");
		
		ChoiceBox<Object> cb = new ChoiceBox<>();
		cb.setItems(FXCollections.observableArrayList(
		    "司机受伤", "汽车损坏 ", "人员伤亡","多种事故")
		);
		TextArea info = new TextArea();
		info.setPrefSize(450,250);

		cb.getSelectionModel().selectedIndexProperty().addListener(
			(ObservableValue<? extends Number> ov,
					Number old_val, Number new_val) -> {
						switch(new_val.intValue()) {
						case 0: 
							insurance = InsuranceFactorys.createInsurance(0);
							break;
						case 1:
							insurance = InsuranceFactorys.createInsurance(1);
							break;
						case 2:
							insurance = InsuranceFactorys.createInsurance(2);
							break;
						case 3:
							insurance = InsuranceFactorys.createInsurance(3);
							break;
						default:
						}
						if(insurance != null) {
							info.setPromptText(insurance.info());
						}
		});
		HBox selectBox = new HBox();
		selectBox.getChildren().addAll(selectType, cb);
		
		VBox vbox = new VBox();
		vbox.getChildren().addAll(personInfo, selectBox);
		vbox.setSpacing(50);

		HBox hbox = new HBox();
		hbox.getChildren().addAll(imageView,vbox);
		hbox.setSpacing(20);
		
		VBox box = new VBox();
		box.getChildren().addAll(hbox,info);
		box.setSpacing(15);
		
		borderPane.setCenter(box);
		BorderPane.setMargin(box,new Insets(10, 10, 0, 10));
		
		getChildren().add(borderPane);
		
	}
}
